<?php
/**
 * Created by PhpStorm.
 * User: lzq
 * Date: 2018-11-8
 * Time: 11:19
 */

namespace backend\models;

use yii;
use yii\base\Model;
use yii\web\UploadedFile;

class UploadForm extends Model
{
    /**
     * @var UploadedFile
     */
    public $file;
    public $filename;


    public function rules()
    {
        return [
            [['file'], 'file', 'skipOnEmpty' => false,  'extensions' => 'xls,xlsx','checkExtensionByMimeType' => false],
        ];
    }

    public function upload()
    {
        if ($this->validate()) {
            $fn = date("YmdHis").rand(1000,9999); //$this->filename->baseName
            $this->filename = Yii::$app->basePath.DIRECTORY_SEPARATOR . "web".DIRECTORY_SEPARATOR."uploads" .DIRECTORY_SEPARATOR .$fn . '.' . $this->file->extension;

            $this->file->saveAs($this->filename );

            return true;
        } else {
            var_dump($this->errors);
            return false;
        }
    }
}